<template>
	<view class="has-pdlr-20">
		<view v-for="(item,index) in list" :key="index" @tap="topage" :data-id="item.g_id" class="notice_list">
			<view class="has-floatl">
				<view>{{item.g_title}}</view>
				<view class="is-grey is-size-14">{{item.g_time}}</view>
			</view>
			<view class="notice_to has-floatr">
				<image src="../../static/img/common/right_ico_grey.png" mode=""></image>
			</view>
		</view>
		<view style="flex: 1;">
			<uni-err-box  :errType="errType" :errText="errText" ></uni-err-box>
		</view>
		<view style="flex: 1;">
			<uni-load-more  :loadingType="loadingType" :contentText="contentText" ></uni-load-more>
		</view>
	</view>
</template>

<script>
	import uniLoadMore from '@/components/load-more.vue';
	import uniErrBox from '@/components/err-box.vue';
	var _self,
		page = 1,
		timer = null;
	export default {
		components: {
			uniLoadMore,
			uniErrBox
		},
		data(){
			return{
				errType: 0,
				errText: '',
				list:[],
				loadingText: '加载中...',
				loadingType: 0,//定义加载方式 0---contentdown  1---contentrefresh 2---contentnomore
				contentText: {
					contentdown:'上拉显示更多',
					contentrefresh: '正在加载...',
					contentnomore: '没有更多了'
				}
			}
		},
		onLoad: function(options) {
			_self = this;
			this.getList();
		},
		//下拉刷新
		onPullDownRefresh: function() {
			this.getList();
		},
		// 加载更多
		onReachBottom : function(){
			if (timer != null) {
				clearTimeout(timer);
			}
			timer = setTimeout(function() {
				_self.getmore();
			}, 1000);
		},
		methods: {
			// 数据和分页是模拟的实际也是这样写
			getmore: function(){
				if (_self.loadingType !== 0) {//loadingType!=0;直接返回
					return false;
				}
				_self.loadingType = 1;
				uni.showNavigationBarLoading();//显示加载动画
				uni.request({
					url: this.service.FOX_URL + 'diamond/question_list',
					method:'POST',
					data: {
						page:page,
						limit:10
					},
					dataType:'json',
					header:{
						'content-type':'application/x-www-form-urlencoded'
					},
					success: res => {
						var foxmsg = res.data;
						if (foxmsg.data.list == null  || foxmsg.data.list.length==0) {//没有数据
							_self.loadingType = 2;
							uni.hideNavigationBarLoading();//关闭加载动画
							return;
						}
						page++;//每触底一次 page +1
						_self.list = _self.list.concat(foxmsg.data.list);//将数据拼接在一起
						_self.loadingType = 0;//将loadingType归0重置
						uni.hideNavigationBarLoading();//关闭加载动画
					},
					complete: res => {
						uni.hideLoading();
						uni.stopPullDownRefresh();
					}
				});
			},
			getList: function() {//第一次回去数据
				page = 1;
				this.loadingType = 0;
				uni.showNavigationBarLoading();
				uni.request({
					url: this.service.FOX_URL + 'diamond/question_list',
					method:'POST',
					data:{
						page:page,
						limit:10
					},
					dataType:'json',
					header:{
						'content-type':'application/x-www-form-urlencoded'
					},
					success: res => {
						page++;//得到数据之后page+1
						var foxmsg = res.data;
						// console.log(foxmsg)
						if (foxmsg.code == 200) {                    
							_self.list = foxmsg.data.list;
							if (foxmsg.data.list.length > 0 && foxmsg.data.list.length < 10) {//没有数据
								_self.loadingType = 5;
								uni.hideNavigationBarLoading();//关闭加载动画
								return;
							}
							if (foxmsg.data.list.length == 0) {
								_self.errType = 1;
								_self.loadingType = 4;
								uni.hideNavigationBarLoading();//关闭加载动画
								return;
							}
							uni.hideNavigationBarLoading();
							uni.stopPullDownRefresh();//得到数据后停止下拉刷新
						}else{
							_self.errType = 1;
							_self.loadingType = 4;
							uni.hideNavigationBarLoading();//关闭加载动画
							return;
						}                         
					}
				});
			},
			topage: function(e){
				var id = e.currentTarget.dataset.id;
				uni.navigateTo({
					url: '/pages/question/question_page?g_id='+id
				})
			}
		}
	}
</script>

<style>
	.notice_list{
		height: 120rpx;
		line-height: 60rpx;
		border-bottom: 2rpx solid #E5E5E5;
		padding:30rpx 0;
	}
	.notice_to image{
		width:60rpx;
		height: 60rpx;
		margin-top: 40rpx;
	}
</style>
